import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:go_router/go_router.dart';

import '../models/user.dart';

// ignore: must_be_immutable
class ChatUserLists extends StatefulWidget {
  List<User> listData;
  ScrollController scrollController;
  ChatUserLists(
      {required this.listData, required this.scrollController, super.key});

  @override
  State<ChatUserLists> createState() => _ChatUserListsState();
}

class _ChatUserListsState extends State<ChatUserLists> {
  @override
  Widget build(BuildContext context) {
    return ListView(
      controller: widget.scrollController,
      padding: const EdgeInsets.all(14),
      children: chatList(context, widget.listData),
    );
  }
}

List<Widget> chatList(BuildContext context, List<User> listData) {
  List<Widget> widgets = [];
  for (var i = 0; i < listData.length; i++) {
    // print(titles[i]);
    widgets.add(
      GestureDetector(
        onTap: () async {
          context
              .pushNamed("chat", pathParameters: {"id": "${listData[i].id}"});
        },
        child: Container(
          margin: const EdgeInsets.fromLTRB(20, 0, 20, 10),
          padding: const EdgeInsets.all(8),
          height: 75,
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.circular(8)),
          child: Row(
            children: [
              Container(
                width: 60,
                height: 60,
                margin: const EdgeInsets.only(right: 20),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8),
                ),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(8),
                  child: Image.network(
                    listData[i].userImg,
                    errorBuilder: (context, error, stackTrace) {
                      return Image.asset("assets/images/netError.jpg");
                    },
                  ),
                ),
              ),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    width: 200,
                    child: Text(
                      listData[i].userName == ""
                          ? "未设置名称"
                          : listData[i].userName,
                      maxLines: 1,
                      style: const TextStyle(
                          fontWeight: FontWeight.w800, fontSize: 20),
                    ),
                  ),
                  Text(
                      "${listData[i].createTime.year}-${listData[i].createTime.month}-${listData[i].createTime.day}")
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  widgets = widgets
      .animate(interval: 50.ms)
      .fadeIn(duration: 600.ms, delay: 200.ms)
      .shimmer(
          blendMode: BlendMode.srcOver, color: Colors.black.withOpacity(0.5))
      .move(begin: const Offset(0, 30), curve: Curves.easeOutQuad);
  return widgets;
}
